package vip.xiaonuo.smzq.modular.kcl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.core.utils.TransFormUtil;
import vip.xiaonuo.smzq.modular.kcl.entity.Zb;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.Coordinates;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KqfwCoordinates;
import vip.xiaonuo.smzq.modular.kcl.mapper.ZbMapper;
import vip.xiaonuo.smzq.modular.kcl.service.IZbService;

import javax.annotation.Resource;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 坐标 服务实现类
 *
 * @author eomer
 * @since 2024-02-22
 */
@Service
public class ZbServiceImpl extends ServiceImpl<ZbMapper, Zb> implements IZbService {

    @Resource
    private ZbMapper zbMapper;

    @Override
    public Page<Zb> page(Zb zb) {
        QueryWrapper<Zb> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }


    /**
     * 勘查区块范围，资源储量计算范围的拐点坐标，标高，面积及示意图
     *
     * @Param kqbm 矿区编码
     * @Param djflbm 登记分类编码
     * @Param nd 年度
     */
    @Override
    public Coordinates getCoordinates(String kqbh, Integer djflbh, Integer nd) {
        //copyf返回对象
        Coordinates coordinates = new Coordinates();
        try {
            //获取坐标详情
            QueryWrapper<Zb> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("kqbh", kqbh).eq("djflbh", djflbh).eq("nd", nd);
            Zb zb = zbMapper.selectOne(queryWrapper);
            if (zb != null) {
                BeanUtils.copyProperties(coordinates, zb);

                if (zb.getKqzb() != null && zb.getKqzb().length() > 0) {
                    //算法1：矿区范围拐点坐标
                    coordinates.setKqfwCoordinatesList(getKqfwCoordinates(zb.getKqzb()));
                    coordinates.setWktkqmj(TransFormUtil.CoordinateDisposeWKT3(zb.getKqzb()));

                } else {
                    coordinates.setKqfwCoordinatesList(new ArrayList<KqfwCoordinates>());
                }
                if (zb.getJszb() != null && zb.getJszb().length() > 0) {
                    //算法2：资源储量计算范围拐点坐标
                    coordinates.setZycljsfwCoordinatesList(getKqfwCoordinates(zb.getJszb()));
                    coordinates.setWktcljsmj(TransFormUtil.CoordinateDisposeWKT3(zb.getJszb()));
                } else {
                    coordinates.setZycljsfwCoordinatesList(new ArrayList<KqfwCoordinates>());
                }
                if (zb.getDlzb() != null && zb.getDlzb().length() > 0) {
                    //算法3：动量坐标
                    coordinates.setDlzbCoordinatesList(getKqfwCoordinates(zb.getDlzb()));
                    coordinates.setWktdlmj(TransFormUtil.CoordinateDisposeWKT3(zb.getDlzb()));
                } else {
                    coordinates.setDlzbCoordinatesList(new ArrayList<KqfwCoordinates>());
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return coordinates;
    }

    /**
     * 根据坐标字符串返回拐点坐标对象集合
     *
     * @param coordinatesStr 坐标字符串
     *                       <p>
     *                       2,
     *                       5,
     *                       1,4421028.05,39571109.36,-----4
     *                       2,4421062.05,39571064.36,
     *                       3,4421110.05,39571178.36,
     *                       4,4421088.05,39571236.36,
     *                       5,4421024.05,39571217.36,
     *                       103,47,,1,
     *                       3,
     *                       6,4420876.05,39571372.36,
     *                       7,4420967.05,39571412.36,
     *                       8,4420819.05,39571421.36,
     *                       103,47,,1,
     * @return
     */
    public List<KqfwCoordinates> getKqfwCoordinates(String coordinatesStr) {
        List<KqfwCoordinates> kqfwCoordinatesList = new ArrayList<>();
        try {
            coordinatesStr = coordinatesStr.trim();
            //转换成数组
            String[] coordinatesArry = coordinatesStr.replaceAll(" ", "").split(",");
            if (coordinatesArry.length < 3) {
                return kqfwCoordinatesList;
            }
            //获取第一个元素判断是几个图形
            Integer graphicCount = Integer.parseInt(coordinatesArry[0]);
            //获取第一个图像的坐标个数
            Integer coordinatesCount = Integer.parseInt(coordinatesArry[1]);

            for (int i = 2; i < coordinatesArry.length; ) {

                KqfwCoordinates kqfwCoordinates = new KqfwCoordinates();
                if (coordinatesCount == 0) {
                    //x
                    kqfwCoordinates.setX(coordinatesArry[i]);
                    //Y
                    kqfwCoordinates.setY(coordinatesArry[i + 1]);
                    //拐点
                    kqfwCoordinates.setInflectionPoint("*");
                    if (coordinatesArry[i + 2] != null) {
                        //矿体标识
                        kqfwCoordinates.setOreBodyIdentification(coordinatesArry[i + 2]);
                    }

                    if (coordinatesArry[i + 3] != null) {
                        //性质
                        kqfwCoordinates.setNature(coordinatesArry[i + 3]);
                    }
                    if ((i + 4) < coordinatesArry.length) {
                        if (coordinatesArry[i + 4] != null && !coordinatesArry[i + 4].equals("")) {
                            // 获取图像的坐标个数
                            coordinatesCount = Integer.valueOf(coordinatesArry[i + 4]);
                        }
                    }
                    i = i + 5;
                } else {
                    //拐点
                    kqfwCoordinates.setInflectionPoint(coordinatesArry[i]);
                    if (coordinatesArry[i + 1] != null) {
                        //x
                        kqfwCoordinates.setX(coordinatesArry[i + 1]);
                    }
                    if (coordinatesArry[i + 2] != null && !coordinatesArry[i + 2].equals("")) {
                        //Y
                        kqfwCoordinates.setY(coordinatesArry[i + 2]);
                    }

                    coordinatesCount--;
                    i = i + 3;
                }

                kqfwCoordinatesList.add(kqfwCoordinates);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return kqfwCoordinatesList;
    }

    /**
     * 获取坐标字符串
     *
     * @param kqfwCoordinatesList
     * @return
     */
    public static String getCoordinatesStr(List<KqfwCoordinates> kqfwCoordinatesList) {
//        String coordinatesStr = "";
//        try{
//            //获取图形数量
//            Integer graphicCount = 0;
//            //获取拐点个数
//            int point=0;
//            //每个图形的所有信息字符串
//            String point2000="";
//            for (int i=0;i<kqfwCoordinatesList.size();i++){
//                KqfwCoordinates kqfwCoordinates=kqfwCoordinatesList.get(i);
//                if (!kqfwCoordinates.getInflectionPoint().equals("*")){
//                    if (point2000.equals("")){
//                        point2000=kqfwCoordinates.getInflectionPoint()+","+kqfwCoordinates.getX()+","+kqfwCoordinates.getY();
//                    }else {
//                        point2000=point2000+","+kqfwCoordinates.getInflectionPoint()+","+kqfwCoordinates.getX()+","+kqfwCoordinates.getY();
//                    }
//                    point++;
//                }else{
//                    coordinatesStr=point+","+point2000+","+kqfwCoordinates.getX()+","+kqfwCoordinates.getY()+","+
//                            kqfwCoordinates.getOreBodyIdentification()+","+kqfwCoordinates.getNature();
//                    graphicCount++;
//                    point=0;
//                    point2000="";
//                }
//
//            }
//            coordinatesStr=graphicCount+","+coordinatesStr;
//        }catch (Exception e){
//
//        }
//        return coordinatesStr;
        String result = "";
        String coordinatesStr = "";
        try {
            //获取图形数量
            Integer graphicCount = 0;
            //获取拐点个数
            int point = 0;
            //每个图形的所有信息字符串
            String point2000 = "";
            List<String> fwList = CollUtil.newArrayList();
            for (int i = 0; i < kqfwCoordinatesList.size(); i++) {
                KqfwCoordinates kqfwCoordinates = kqfwCoordinatesList.get(i);
                if (!kqfwCoordinates.getInflectionPoint().equals("*")) {
                    if (point2000.equals("")) {
                        point2000 = kqfwCoordinates.getInflectionPoint() + "," + kqfwCoordinates.getX() + "," + kqfwCoordinates.getY();
                    } else {
                        point2000 = point2000 + "," + kqfwCoordinates.getInflectionPoint() + "," + kqfwCoordinates.getX() + "," + kqfwCoordinates.getY();
                    }
                    point++;
                } else {
                    coordinatesStr = point + "," + point2000 + "," + kqfwCoordinates.getX() + "," + kqfwCoordinates.getY() + "," +
                            kqfwCoordinates.getOreBodyIdentification() + "," + kqfwCoordinates.getNature();
                    fwList.add(coordinatesStr);
                    graphicCount++;
                    point = 0;
                    point2000 = "";
                }

            }
            result = graphicCount + "," + CollUtil.join(fwList, ",");
        } catch (Exception e) {
            System.out.println(e);
        }
        return result;
    }
//    public static void main(String[] args) {
//        List<KqfwCoordinates> nums= JSONUtil.toList(ResourceUtil.readUtf8Str("json" + File.separator + "coordinates.json"), KqfwCoordinates.class);
//        System.out.println("返回值："+getCoordinatesStr(nums));
//
//
//
//    }


    @Override
    public Integer addZb(Coordinates coordinates) {
        Integer count = 0;
        try {
            //获取坐标详情
            QueryWrapper<Zb> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("kqbh", coordinates.getKqbh()).eq("djflbh", coordinates.getDjflbh()).eq("nd", coordinates.getNd());
            Zb zb = zbMapper.selectOne(queryWrapper);
            Zb zbNew = new Zb();
            try {
                if (zb == null) {
                    BeanUtils.copyProperties(zbNew, coordinates);
                } else {
                    BeanUtils.copyProperties(zbNew, zb);
                }

                if (coordinates.getZycljsfwCoordinatesList() != null) {
                    zbNew.setJszb(getCoordinatesStr(coordinates.getZycljsfwCoordinatesList()));
                }
                if (coordinates.getKqfwCoordinatesList() != null) {
                    zbNew.setKqzb(getCoordinatesStr(coordinates.getKqfwCoordinatesList()));
                }
                if (coordinates.getDlzbCoordinatesList() != null) {
                    //动量坐标
                    zbNew.setDlzb(getCoordinatesStr(coordinates.getDlzbCoordinatesList()));
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            if (zb == null) {
                count = zbMapper.insert(zbNew);

            } else {
                count = zbMapper.updateById(zbNew);
            }
        } catch (Exception e) {
            System.out.println(e);
        }


        return count;
    }

    @Override
    public List<Zb> selectList(QueryWrapper<Zb> queryWrapperZb) {
        return zbMapper.selectList(queryWrapperZb);
    }

    @Override
    public void delect(String kqbh, Integer djflbh, Integer nd) {
        QueryWrapper<Zb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kqbh", kqbh).eq("djflbh", djflbh).eq("nd", nd);
        zbMapper.delete(queryWrapper);
    }

}
